package com.cs.beneficios.web.action;
import com.cs.beneficios.business.entity.AuditoriaDAO;
import com.cs.beneficios.business.model.SubProgramaVO;
import com.cs.beneficios.common.ConnectionFactory;

import com.cs.paoyer.administration.business.model.UserVO;
import java.io.IOException;

import java.sql.SQLException;
//import java.util.ArrayList;
import java.util.Collection;
//import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class CAGenerateReportAction extends Action 
{
  /**
   * This is the main action called from the Struts framework.
   * @param mapping The ActionMapping used to select this instance.
   * @param form The optional ActionForm bean for this request.
   * @param request The HTTP Request we are processing.
   * @param response The HTTP Response we are processing.
   * @return 
   * @throws java.io.IOException
   * @throws javax.servlet.ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
  {
    //ConnectionFactory cf = new ConnectionFactory("beneficios","beneficios");
	ConnectionFactory cf = new ConnectionFactory();
    HttpSession session = request.getSession();
    UserVO user = (UserVO) request.getSession().getAttribute("userInfo");
    
    String strAdmin = (String)request.getParameter("administrador");
    String strCodeAdmin = (String)request.getParameter("admincodigo");
    String strCodeProg = (String)request.getParameter("codePrograma");
    String strCodeSubprog = (String)request.getParameter("code");
    String strDesde = (String)request.getParameter("desde");
    String strHasta = (String)request.getParameter("hasta");
    String strDescription = null;
    Collection cReport = null;
    
    session.removeAttribute("cReport");
    
    /*List lUsers = (List)session.getAttribute("cUsers");
    UserVO objUserVO = new UserVO();
    objUserVO = (UserVO)lUsers.get(0);
    String strDescription = objUserVO.getDescription();*/
    
        
    SubProgramaVO objSubpro = new SubProgramaVO();
    if(strAdmin != null)
      objSubpro.setNombreUsuario(strAdmin);
    if(strCodeProg != null && !strCodeProg.equals(""))
      objSubpro.setCodePrograma(new Long(strCodeProg));
    if(strCodeSubprog != null && !strCodeSubprog.equals(""))
      objSubpro.setCodeSubPrograma(strCodeSubprog);
    if(strDesde != null && !strDesde.equals(""))
      objSubpro.setDesdeVigencia(strDesde);
    if(strHasta != null && !strHasta.equals(""))
      objSubpro.setHastaVigencia(strHasta);
    try
    {
      AuditoriaDAO objAudiDAO = new AuditoriaDAO(cf);
      strDescription = objAudiDAO.getDescriptionUser(strAdmin);
      cReport = objAudiDAO.getReportControl(objSubpro);
      cf.closeConnection(objAudiDAO.getConn());
      
      if(cReport != null && cReport.size() != 0)
        session.setAttribute("cReport",cReport);
      
      if(strAdmin != null && !strAdmin.equals(""))
        request.setAttribute("usuario",strAdmin);
      if(strDescription != null && !strDescription.equals(""))
        request.setAttribute("descripcion",strDescription);
      if(strDesde != null && !strDesde.equals(""))
      {
        String ArrFecha[] = strDesde.split("-");
        String strDay = ArrFecha[2];
        String strMonth = ArrFecha[1];                       
        String strYear = ArrFecha[0];
        String strDesdeFinal = strDay+"/"+strMonth+"/"+strYear;
        request.setAttribute("desde",strDesdeFinal);
      }
        
      if(strHasta != null && !strHasta.equals(""))
      {
        String ArrFecha[] = strHasta.split("-");
        String strDay = ArrFecha[2];
        String strMonth = ArrFecha[1];                       
        String strYear = ArrFecha[0];
        String strHastaFinal = strDay+"/"+strMonth+"/"+strYear;
        request.setAttribute("hasta",strHastaFinal); 
      }
        
        
    }catch(SQLException sqle)
    {
		System.err.println("/******** TDSUB - cwsub");
		sqle.printStackTrace();
		System.err.println("TDSSPE - cwsub ********/");
    }
    
    
    
    return mapping.findForward("success");
  }
}